package com.pjnkj.thunder.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pjnkj.thunder.common.json.Body;
import com.pjnkj.thunder.common.json.Meta;
import com.pjnkj.thunder.entity.Shop;
import com.pjnkj.thunder.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.pjnkj.thunder.common.json.Meta.CODE_470;

/**
* <p>
*  前端控制器
* </p>
*
* @author pjnkj
* @since 2022-01-20
*/
@RestController
@RequestMapping("/thunder/shop")
    public class ShopController {
    @Autowired
    ShopService shopService;

    /**
    * 删除数据
    *
    * @param shop
    */
    @PostMapping("delete")
    public Body delete(Integer id) {
        Boolean flag = shopService.removeById(id);
        if (flag == true) {
            return Body.newInstance("删除成功");
        }
        return Body.newInstance(Meta.CODE_400, "删除失败");
    }

    /**
    * 增加数据
    *
    * @param shop
    */
    @PostMapping("create")
    public Body create(@Validated(Shop.class) @RequestBody Shop shop) {
        Boolean flag = shopService.saveOrUpdate(shop);
        if (flag == true) {
            return Body.newInstance("保存成功");
        }
        return Body.newInstance(Meta.CODE_400, "保存失败");
    }

    /**
    * 修改数据
    *
    * @param shop
    */
    @PostMapping("update")
    public Body update(@Validated(Shop.class) @RequestBody Shop shop) {
        Boolean flag = shopService.updateById(shop);
        if (flag == true) {
            return Body.newInstance("修改成功");
        }
        return Body.newInstance(Meta.CODE_400, "修改失败");
    }

    /**
    * 查询数据
    *
    * @param
    */
    @PostMapping("find")
    public Body find(Integer id) {
        Shop info = shopService.getById(id);
        if (info != null) {
            return Body.newInstance(info);
        }
        return Body.newInstance(CODE_470, "查询失败");
    }


    /**
    * 查询数据list
    *
    * @param
    */
    @PostMapping("findlist")
    public Body findlist() {
        List<Shop> list = shopService.list();
        if (list != null && list.size() > 0) {
            return Body.newInstance(list);
        }
        return Body.newInstance(CODE_470, "查询失败");
    }

    /**
    * 分页查询数据
    *
    * @param
    */
    @PostMapping("page")
    public Body page(Integer current, Integer size) {
        Page<Shop> pg = new Page<Shop>(current, size);
        IPage<Shop> list = shopService.page(pg);
        if (list != null && list.getSize() > 0) {
            return Body.newInstance(list);
        }
        return Body.newInstance(CODE_470, "查询失败");
    }
}
